<template>
  <div class="bigbox">
   <!-- 页面导航搜索盒子 -->
    <div class="topbox">
      <el-form ref="form"  label-width="60px" label-position="left">
        <el-row :gutter="20">
          <el-col :span="5">
            <el-form-item label="建筑群">
              <FilterSelect ref="jianzhuqun" :optionlist="qiyelist" :valuekey="{name:'name',value:'id'}" @changeselect="changeqiye" :defaultvalue="buildingId"></FilterSelect>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="用户">
              <FilterSelect ref="yonghu" :optionlist="yomghulist" :valuekey="{name:'customerName',value:'id'}" ></FilterSelect>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="状态">
              <el-select size="small" v-model="resetFlag" style="width: 100%;" clearable>
                <el-option label="未消警" value="0" class="option"></el-option>
                <el-option label="已消警" value="1" class="option"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="日期">
              <el-date-picker
                size="small"
                style="width: 100%;"
                v-model="monthvalue"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="3" style="text-align: right;padding-top: 4px;">
              <el-button size="small" @click="chongzhi">重 置</el-button>
              <el-button type="primary" size="small" @click="gettablelist()">查 询</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div> 
    <!-- 盒子主体部分 -->
    <div class="bodybox">
      <div class="title">
        <div>异常用户</div>
        <div><TopTile  @command="command" @daochu="daochu" @shuaxin="shuaxin"></TopTile></div>
      </div>
      <div class="table">
        <TableColumn
        ref="tabledata"
        :tableData="tableData" 
        :tableConfig="tableConfig" 
        :issize="issize"></TableColumn>
      </div>
        <!-- 底部分页 -->
        <div class="fenye">
          <el-pagination
                background
                layout="prev, pager, next"
                :current-page.sync="pageobj.pageNo"
                :page-size="20"
                @current-change="gettablelist"
                :total="+totalCount">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import FilterSelect from '@/components/FilterSelect'
import {gettablelistapis} from '@/api/prwpaid/user-manage/abnormal-user'
import {gettablelistapi} from '@/api/prwpaid/user-manage/OpenAaccount'
import {getloginlistapi} from '@/api/prwpaid/file-management/Architectural-complex'
import TopTile from '@/components/TopTitle/index.vue'
import TableColumn from '@/components/Table/index.vue'
import {exportexcel} from '@/utils/excel'
export default {
  name: 'CallPoliceIndex',
  components: {
    TopTile,
    TableColumn,
    FilterSelect
  },
  data() {
    return {
      resetFlag:'',
      issize: 'medium',
      monthvalue:['',''],
      yomghulist: [],
      qiyelist: [],
      tableData: [],
      tableConfig: [
      { label: '序号', type:'index' ,center:'center',width:'100'},
        { label: '建筑群名称', prop: 'buildingName',width:'200' },
        { label: '用户名称', prop: 'customerName' ,width:'250'},
        { label: '用户编号', prop: 'customerCode' },
        { label: '异常记录', prop: 'alarmRecord' ,width:'450'},
        { label: '记录时间', prop: 'recordTime' ,center:'center'},
      ],
      pageobj: {
        pageNo: 1,
        pageSize:20
      },
      totalCount: '',
      buildingId:''
    };
  },

  async mounted() {

    const res = await getloginlistapi()
    this.qiyelist = res.data.list
    const localbuildingId = localStorage.getItem('buildingId')
    const isBuildIdInArray = this.qiyelist.some(item => item.id == localbuildingId);
    if(isBuildIdInArray){
      this.buildingId = localbuildingId ? localbuildingId : ''
      this.getyonghuxialalist(this.buildingId)
    }else{
      this.$set(this,'buildingId',isBuildIdInArray?localbuildingId:this.qiyelist[0].id)
    }

    this.gettablelist()
  },

  methods: {
    // 企业下拉
    changeqiye(val) {
      localStorage.setItem('buildingId', val)
      this.getyonghuxialalist(val)
    },
    // 获取用户
    async getyonghuxialalist(val) {
    const ress = await gettablelistapi({
        pageNo: 0, pageSize: 500,
      startDate: '', endDate: '', 
      state:'20',buildingId:val,customerType:''
    })
    this.yomghulist = ress.data.list
    },
    // 获取表格数据
    async gettablelist() {
      this.$refs.tabledata.loading=true
      const { data: { list,totalCount } } = await gettablelistapis({
        buildingId: this.$refs.jianzhuqun.brandId, customerId: this.$refs.yonghu.brandId,
        pageNo: this.pageobj.pageNo-1, pageSize: this.pageobj.pageSize,
        startDate:this.monthvalue[0], endDate:this.monthvalue[1],resetFlag:this.resetFlag
      })
      this.tableData = list
      this.totalCount = totalCount
      this.$refs.tabledata.loading=false
    },
    // 重置
    chongzhi() {
      localStorage.removeItem('buildingId');
      this.resetFlag=''
      this.$refs.jianzhuqun.brandId = ''
      this.$refs.yonghu.brandId = ''
      this.monthvalue=['','']
      this.gettablelist()
    },
 // 调节表格密度
  command(val) {
      if (val === '默认') {
        this.issize = 'medium'
      } else if (val === '中等') {
        this.issize = 'small'
      } else {
        this.issize = 'mini'
      }
    },
    // 导出
    daochu() {
      exportexcel('mbm-microservice-biz/mbm/cost/balance/alarm/exportExcel',{
      buildingId: this.buildingId, customerId: this.customerId,
        startDate:this.monthvalue[0], endDate:this.monthvalue[1]
      },'区域房间')
    },
    // 刷新列表
    shuaxin() {
      this.gettablelist()
      this.$message.success('列表刷新成功')
    },
  },
};
</script>

<style lang="scss" scoped>
.option{
  padding-left: 10px;
}
.bigbox{
  height: 100%;
  padding: 10px;
  .topbox{
    height: 80px;
    padding: 20px;
    background-color: #fff;
    margin-bottom: 10px;
    span{
      margin-right: 15px;
    }
    .text{
      font-size: 14px;
      color: #606266;
    }
  }
  .bodybox{
    height: calc(100% - 90px);
    padding: 20px;
    background-color: #fff;
    .title{
      margin-bottom: 10px;
      display:flex;
      justify-content:space-between;
    }
    .table{
      height: 90%;
    }
    .fenye{
      text-align: right;
    }
  }
}
</style>